<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
<div id="app">
    <bro1></bro1>
    <bro2></bro2>
</div>
</body>

</html>
<!-- bro1结构 -->
<template id="bro1">
    <fieldset>
        <legend>兄die组件1</legend>
        <input type="text" v-model="msg">
        <button @click="trigger">点击</button>
    </fieldset>
</template>
<!-- bro2结构 -->
<template id="bro2">
    <fieldset>
        <legend>兄die组件2</legend>
        <ul>
            <li v-for="(item,i) in arr" :key="i">{{item}}</li>
        </ul>
    </fieldset>
</template>
<!-- <script src="./tinyEmitter.js"></script> -->
<script src='https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js'></script>
<script>
    //实例化TinyEmitter

    let bus = new Vue;

    // 注册兄弟组件1
    Vue.component('bro1', {
        template: "#bro1",
        data() {
            return {
                msg: ""
            }
        },
        //事件处理函数
        methods: {
            trigger() {
                //触发注册的事件(发布)
                bus.$emit('res', this.msg)
            }
        }
    })
    // 注册兄弟组件2
    Vue.component('bro2', {
        template: "#bro2",
        data() {
            return {
                arr: ['吃']
            }
        },
        //先注册事件(添加订阅)
        //组件的钩子函数
        mounted() {
            bus.$on('res', (input) => {
                //接收兄弟组件传递的参数
                console.log(input)
                this.arr.push(input)
            })
        }
    })


    let vm = new Vue({
        el: "#app"
    })
</script>